package com.irdstudio.efp.rule.service.impl;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.efp.rule.service.dao.TaxAuthInfoDao;
import com.irdstudio.efp.rule.service.dao.TaxBasicInfoDao;
import com.irdstudio.efp.rule.service.domain.TaxAuthInfo;
import com.irdstudio.efp.rule.service.domain.TaxBasicInfo;
import com.irdstudio.efp.rule.service.facade.TaxAllInfoService;
import com.irdstudio.efp.rule.service.vo.TaxBasicInfoVO;
import com.irdstudio.efp.rule.service.vo.TaxInfoMainVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service("taxAllInfoService")
public class TaxAllInfoServiceImpl implements TaxAllInfoService, FrameworkService{
	//日志记录器
	private static Logger logger = LoggerFactory.getLogger(RuleCallServiceImpl.class);
	
	//纳税授权信息表
	@Autowired
	private TaxAuthInfoDao taxAuthInfoDao;
	
	//纳税基本信息表
	@Autowired
	private TaxBasicInfoDao taxBasicInfoDao;
	@Override
	public List<TaxInfoMainVO> queryAllInfoByConditon(TaxInfoMainVO taxInfoMainVO) {
		logger.info("正在查询纳税信息");
		List<TaxInfoMainVO> taxInfoMainVOList = new ArrayList<TaxInfoMainVO>();
		/**List<TaxBasicInfo> taxBasicInfoList = new ArrayList<TaxBasicInfo>();
		//查询纳税基本信息表
		TaxBasicInfo taxBasicInfo = new TaxBasicInfo();
		beanCopy(taxInfoMainVO,taxBasicInfo);	//将请求bean中的参数设置到taxBasicInfo中
		taxBasicInfo.setCreateTime(taxInfoMainVO.getApplyDt());
		taxBasicInfoList = Optional.ofNullable(taxBasicInfoDao.queryAllByConditionByPage(taxBasicInfo)).orElseGet(ArrayList::new);
		pageSet(taxBasicInfoList,taxBasicInfo);
		//查询授权信息
		TaxAuthInfo taxAuthInfo = new TaxAuthInfo();
		for(TaxBasicInfo innerTaxBasicInfo : taxBasicInfoList) {
			TaxAuthInfo innerTaxAuthInfo = new TaxAuthInfo();
			taxAuthInfo.setCrdtAppFlowNo(innerTaxBasicInfo.getCrdtAppFlowNo());
			innerTaxAuthInfo = taxAuthInfoDao.queryByCrdtAppFlowNo(taxAuthInfo);
			TaxInfoMainVO innertaxInfoMainVO = null;
			innertaxInfoMainVO = transferBean(innerTaxBasicInfo,innerTaxAuthInfo);
			if(innertaxInfoMainVO != null) {
				taxInfoMainVOList.add(innertaxInfoMainVO);
			}
		}*/
		
		return taxInfoMainVOList;
	}

	private TaxInfoMainVO transferBean(TaxBasicInfo taxBasicInfo, TaxAuthInfo taxAuthInfo) {
		TaxInfoMainVO taxInfoMainVO = new TaxInfoMainVO();
		beanCopy(taxBasicInfo, taxInfoMainVO);
		if(taxAuthInfo != null) {
			//将主表中的信息全部拷贝到返回的TaxInfoMainVO类的对象中
			taxInfoMainVO.setAuthBank(taxAuthInfo.getAuthBank());
			taxInfoMainVO.setAuthDt(taxAuthInfo.getAuthDt());
			taxInfoMainVO.setAuthEndDt(taxAuthInfo.getAuthEndDt());
			taxInfoMainVO.setAuthFlowNo(taxAuthInfo.getAuthFlowNo());
			taxInfoMainVO.setAuthrCertNo(taxAuthInfo.getAuthrCertNo());
			taxInfoMainVO.setApplyDt(taxBasicInfo.getCreateTime().substring(0, 10));
			taxInfoMainVO.setCertNo(taxBasicInfo.getCertNo());
			taxInfoMainVO.setCrdtAppFlowNo(taxBasicInfo.getCrdtAppFlowNo());
			taxInfoMainVO.setTaxpyNm(taxBasicInfo.getTaxpyNm());
		}
		return taxInfoMainVO;
	}

	@Override
	public TaxBasicInfoVO queryByCrdtAppFlowNo(TaxBasicInfoVO inTaxBasicInfoVO) {
		logger.debug("当前查询参数信息为:"+ inTaxBasicInfoVO);
		try {
			TaxBasicInfo queryTaxBasicInfo = new TaxBasicInfo();
			beanCopy(inTaxBasicInfoVO, queryTaxBasicInfo);
			TaxBasicInfo queryRslTaxBasicInfo = taxBasicInfoDao.queryByCrdtAppFlowNo(queryTaxBasicInfo);
			if (Objects.nonNull(queryRslTaxBasicInfo)) {
				TaxBasicInfoVO outTaxBasicInfoVo = beanCopy(queryRslTaxBasicInfo, new TaxBasicInfoVO());
				logger.debug("当前查询结果为:"+ outTaxBasicInfoVo.toString());
				return outTaxBasicInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public List<TaxBasicInfoVO> queryAllOwner(TaxBasicInfoVO taxBasicInfoVO) {
		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<TaxBasicInfoVO> list = null;
		try {
			List<TaxBasicInfo> taxBasicInfoVOS = taxBasicInfoDao.queryAllOwnerByPage(taxBasicInfoVO);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + taxBasicInfoVOS.size());
			pageSet(taxBasicInfoVOS, taxBasicInfoVO);
			list = (List<TaxBasicInfoVO>) beansCopy(taxBasicInfoVOS, TaxBasicInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<TaxBasicInfoVO> queryAllCurrOrg(TaxBasicInfoVO taxBasicInfoVO) {
		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<TaxBasicInfoVO> list = null;
		try {
			List<TaxBasicInfo> taxBasicInfoVOS = taxBasicInfoDao.queryAllCurrOrgByPage(taxBasicInfoVO);
			logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + taxBasicInfoVOS.size());
			pageSet(taxBasicInfoVOS, taxBasicInfoVO);
			list = (List<TaxBasicInfoVO>) beansCopy(taxBasicInfoVOS, TaxBasicInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<TaxBasicInfoVO> queryAllCurrDownOrg(TaxBasicInfoVO taxBasicInfoVO) {
		logger.debug("当前查询本人所属机构及下属机构数据信息的参数信息为:");
		List<TaxBasicInfoVO> list = null;
		try {
			List<TaxBasicInfo> taxBasicInfoVOS = taxBasicInfoDao.queryAllCurrDownOrgByPage(taxBasicInfoVO);
			logger.debug("当前查询本人所属机构及下属机构数据信息的结果集数量为:" + taxBasicInfoVOS.size());
			pageSet(taxBasicInfoVOS, taxBasicInfoVO);
			list = (List<TaxBasicInfoVO>) beansCopy(taxBasicInfoVOS, TaxBasicInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<TaxBasicInfoVO> queryAllCurrOwnerPrd(TaxBasicInfoVO taxBasicInfoVO) {
		logger.debug("当前产品种类下数据信息的参数信息为:");
		List<TaxBasicInfoVO> list = null;
		try {
			List<TaxBasicInfo> taxBasicInfoVOS = taxBasicInfoDao.queryAllCurrOwnerPrdByPage(taxBasicInfoVO);
			logger.debug("当当前产品种类下数据信息的结果集数量为:" + taxBasicInfoVOS.size());
			pageSet(taxBasicInfoVOS, taxBasicInfoVO);
			list = (List<TaxBasicInfoVO>) beansCopy(taxBasicInfoVOS, TaxBasicInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<TaxInfoMainVO> queryAllInfoLimit(TaxInfoMainVO taxInfoMainVO, String dataRuleLimit) {
		logger.info("正在查询纳税信息，数据权限为："+dataRuleLimit);
		List<TaxInfoMainVO> taxInfoMainVOList = new ArrayList<TaxInfoMainVO>();
		List<TaxBasicInfo> taxBasicInfoVOList = new ArrayList<TaxBasicInfo>();
		TaxBasicInfoVO taxBasicInfoVO = new TaxBasicInfoVO();
		//将请求bean中的参数设置到taxBasicInfo中
		beanCopy(taxInfoMainVO,taxBasicInfoVO);
		taxBasicInfoVO.setCreateTime(taxInfoMainVO.getApplyDt());
		taxBasicInfoVOList = Optional.ofNullable(this.dataRuleFilterByPage(taxBasicInfoVO, dataRuleLimit)).orElseGet(ArrayList::new);
		pageSet(taxBasicInfoVOList, taxBasicInfoVO);
		//查询授权信息
		TaxAuthInfo taxAuthInfo = new TaxAuthInfo();
		for(TaxBasicInfo innerTaxBasicInfo : taxBasicInfoVOList) {
			TaxAuthInfo innerTaxAuthInfo = new TaxAuthInfo();
			taxAuthInfo.setCrdtAppFlowNo(innerTaxBasicInfo.getCrdtAppFlowNo());
			innerTaxAuthInfo = taxAuthInfoDao.queryByCrdtAppFlowNo(taxAuthInfo);
			TaxInfoMainVO innertaxInfoMainVO = null;
			innertaxInfoMainVO = transferBean(innerTaxBasicInfo, innerTaxAuthInfo);
			if(innertaxInfoMainVO != null) {
				taxInfoMainVOList.add(innertaxInfoMainVO);
			}
		}

		return taxInfoMainVOList;
	}

	private List<TaxBasicInfo> dataRuleFilterByPage(TaxBasicInfoVO queryTaxBasicInfo, String dataRuleType){
		List<TaxBasicInfo> result = null;
		switch (dataRuleType) {
			case "01":
				result = taxBasicInfoDao.queryAllOwnerByPage(queryTaxBasicInfo);
				break;
			case "02":
				result = taxBasicInfoDao.queryAllCurrOrgByPage(queryTaxBasicInfo);
				break;
			case "03":
				result = taxBasicInfoDao.queryAllCurrDownOrgByPage(queryTaxBasicInfo);
				break;
			case "04":
				result = taxBasicInfoDao.queryAllCurrOwnerPrdByPage(queryTaxBasicInfo);
				break;
			default:
				result = taxBasicInfoDao.queryAllCurrDownOrgByPage(queryTaxBasicInfo);
				break;
		}
		return result;
	}

}
